e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
1b10db2002-10-08Martin Nilsson 
1f606b1997-01-28Henrik Grubbström (Grubba) /* * Password handling for Pike. * * Henrik Grubbström 1997-01-28
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  * Fixed to be semi-thread-safe by Hubbe. 1998-04-06 * Notice: the *pw* and *gr* functions are NEVER really thread * safe. If some other function executes a *pw* function * without locking the password_protection_mutex, we are * pretty much screwed.
3889db1998-04-17Henrik Grubbström (Grubba)  *
ef01a81998-07-20Henrik Grubbström (Grubba)  * NOTE: To avoid deadlocks, any locking/unlocking of password_protection_mutex * MUST be done with LOCK_IMUTEX()/UNLOCK_IMUTEX().
1f606b1997-01-28Henrik Grubbström (Grubba)  */ /* * Includes */
b788cd1998-07-04Henrik Grubbström (Grubba) #include "global.h"
1f606b1997-01-28Henrik Grubbström (Grubba) #include "system_machine.h" #include "system.h"
38f0221997-12-07Henrik Grubbström (Grubba) #include "module_support.h" #include "interpret.h" #include "stralloc.h" #include "threads.h" #include "svalue.h" #include "builtin_functions.h"
57ee3a1998-04-06Fredrik Hübinette (Hubbe) #include "constants.h"
1f606b1997-01-28Henrik Grubbström (Grubba)  #ifdef HAVE_PASSWD_H # include <passwd.h>
1c9eff1997-09-07Per Hedbor # include <group.h>
1f606b1997-01-28Henrik Grubbström (Grubba) #endif /* HAVE_PASSWD_H */
51dc981998-03-03Martin Stjernholm 
1f606b1997-01-28Henrik Grubbström (Grubba) #ifdef HAVE_PWD_H # include <pwd.h> #endif /* HAVE_PWD_H */
51dc981998-03-03Martin Stjernholm  #ifdef HAVE_GRP_H # include <grp.h> #endif /* HAVE_GRP_H */
1f606b1997-01-28Henrik Grubbström (Grubba) #ifdef HAVE_SHADOW_H # include <shadow.h> #endif /* HAVE_SHADOW_H */
57ee3a1998-04-06Fredrik Hübinette (Hubbe) 
6ad2372002-05-11Martin Nilsson #define sp Pike_sp
57ee3a1998-04-06Fredrik Hübinette (Hubbe) /* * Emulation */
a91ca01998-07-10Henrik Grubbström (Grubba) DEFINE_IMUTEX(password_protection_mutex);
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  #ifdef HAVE_GETPWENT #ifndef HAVE_GETPWNAM struct passwd *getpwnam(char *name) { struct passwd *pw; setpwent(); while(pw=getpwent()) if(strcmp(pw->pw_name,name)) break; endpwent(); return pw; } #define HAVE_GETPWNAM #endif #ifndef HAVE_GETPWUID struct passwd *getpwuid(int uid) { struct passwd *pw; setpwent(); while(pw=getpwent()) if(pw->pw_uid == uid) break; endpwent(); return 0; } #define HAVE_GETPWUID #endif #endif #ifdef HAVE_GETGRENT #ifndef HAVE_GETGRNAM struct group *getgrnam(char *name) { struct group *gr; setgrent(); while(pw=getgrent()) if(strcmp(gr->gr_name,name)) break; endgrent(); return gr; } #define HAVE_GETGRNAM #endif #endif
26a0062002-03-01Marcus Agehall 
8e441e2004-09-27Henrik Grubbström (Grubba) #define SAFE_PUSH_TEXT(X) do { \ char *text_ = (X); \ if(text_) push_text(text_); \
7863d62005-05-06Martin Nilsson  else push_empty_string(); \
8e441e2004-09-27Henrik Grubbström (Grubba)  } while(0)
444d182004-09-26Marcus Comstedt 
1f606b1997-01-28Henrik Grubbström (Grubba) /* * Functions */
51dc981998-03-03Martin Stjernholm #if defined(HAVE_GETPWNAM) || defined(HAVE_GETPWUID) || defined(HAVE_GETPWENT)
704d841998-07-20Fredrik Hübinette (Hubbe) 
57ee3a1998-04-06Fredrik Hübinette (Hubbe) void push_pwent(struct passwd *ent)
1f606b1997-01-28Henrik Grubbström (Grubba) {
286fca1999-05-26Henrik Grubbström (Grubba)  /* NOTE: password_protection_mutex is always locked * when this function is called. */
1f606b1997-01-28Henrik Grubbström (Grubba)  if(!ent) { push_int(0); return; }
704d841998-07-20Fredrik Hübinette (Hubbe)  SAFE_PUSH_TEXT(ent->pw_name);
286fca1999-05-26Henrik Grubbström (Grubba) #if defined(HAVE_GETSPNAM) || defined(HAVE_GETSPNAM_R)
7edf4a2002-03-04Henrik Grubbström (Grubba)  if(!strcmp(ent->pw_passwd, "x") || !strcmp(ent->pw_passwd, "*NP*"))
1f606b1997-01-28Henrik Grubbström (Grubba)  {
6531361999-05-26Henrik Grubbström (Grubba)  /* 64-bit Solaris 7 SIGSEGV's with an access to address 0xffffffff * if the user is not root: * * Cannot access memory at address 0xffffffff. * (gdb) bt * #0 0xff3b9d18 in ?? () * #1 0xff3bd004 in ?? () * #2 0xff3bd118 in ?? () * #3 0xff14b484 in SO_per_src_lookup () from /usr/lib/libc.so.1 * #4 0xff14a1d0 in nss_get_backend_u () from /usr/lib/libc.so.1 * #5 0xff14a9ac in nss_search () from /usr/lib/libc.so.1 * #6 0xff18fbac in getspnam_r () from /usr/lib/libc.so.1 * * /grubba 1999-05-26 */
1f606b1997-01-28Henrik Grubbström (Grubba)  struct spwd *foo;
286fca1999-05-26Henrik Grubbström (Grubba) #ifdef HAVE_GETSPNAM_R struct spwd bar; /* NOTE: buffer can be static since this function is * monitored by the password_protection_mutex mutex. * /grubba 1999-05-26 */ static char buffer[2048];
1bf0631999-05-27Henrik Grubbström (Grubba) 
286fca1999-05-26Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
1bf0631999-05-27Henrik Grubbström (Grubba)  #ifdef HAVE_SOLARIS_GETSPNAM_R
286fca1999-05-26Henrik Grubbström (Grubba)  foo = getspnam_r(ent->pw_name, &bar, buffer, sizeof(buffer));
1bf0631999-05-27Henrik Grubbström (Grubba) #else /* !HAVE_SOLARIS_GETSPNAM_R */ /* Assume Linux-style getspnam_r(). * It would be nice if the function was documented... * /grubba 1999-05-27 */ foo = NULL; if (getspnam_r(ent->pw_name, &bar, buffer, sizeof(buffer), &foo) < 0) { foo = NULL; } #endif /* HAVE_SOLARIS_GETSPNAM_R */
286fca1999-05-26Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
1bf0631999-05-27Henrik Grubbström (Grubba) 
286fca1999-05-26Henrik Grubbström (Grubba) #else /* !HAVE_GETSPNAM_R */ /* getspnam() is MT-unsafe! * /grubba 1999-05-26 */ /* THREADS_ALLOW_UID(); */
ef1e931998-03-26Henrik Grubbström (Grubba)  foo = getspnam(ent->pw_name);
286fca1999-05-26Henrik Grubbström (Grubba)  /* THREADS_DISALLOW_UID(); */ #endif /* HAVE_GETSPNAM_R */
ef1e931998-03-26Henrik Grubbström (Grubba)  if(foo)
1f606b1997-01-28Henrik Grubbström (Grubba)  push_text(foo->sp_pwdp); else
5e9fc02015-08-18Per Hedbor  push_static_text("x");
26a0062002-03-01Marcus Agehall  } else
1f606b1997-01-28Henrik Grubbström (Grubba) #endif /* Shadow password support */
704d841998-07-20Fredrik Hübinette (Hubbe)  SAFE_PUSH_TEXT(ent->pw_passwd);
1f606b1997-01-28Henrik Grubbström (Grubba)  push_int(ent->pw_uid); push_int(ent->pw_gid);
704d841998-07-20Fredrik Hübinette (Hubbe) 
9d82551998-05-22Henrik Grubbström (Grubba) #ifdef HAVE_PW_GECOS
704d841998-07-20Fredrik Hübinette (Hubbe)  SAFE_PUSH_TEXT(ent->pw_gecos);
9d82551998-05-22Henrik Grubbström (Grubba) #else /* !HAVE_PW_GECOS */
5e9fc02015-08-18Per Hedbor  push_static_text("Mister Anonymous");
9d82551998-05-22Henrik Grubbström (Grubba) #endif /* HAVE_PW_GECOS */
704d841998-07-20Fredrik Hübinette (Hubbe)  SAFE_PUSH_TEXT(ent->pw_dir); SAFE_PUSH_TEXT(ent->pw_shell);
1f606b1997-01-28Henrik Grubbström (Grubba)  f_aggregate(7); }
51dc981998-03-03Martin Stjernholm #endif
1c9eff1997-09-07Per Hedbor 
40a7c01999-03-08Martin Stjernholm #if defined(HAVE_GETGRNAM) || defined(HAVE_GETGRGID) || defined(HAVE_GETGRENT)
57ee3a1998-04-06Fredrik Hübinette (Hubbe) void push_grent(struct group *ent)
1c9eff1997-09-07Per Hedbor { if(!ent) { push_int(0); return; }
704d841998-07-20Fredrik Hübinette (Hubbe)  SAFE_PUSH_TEXT(ent->gr_name); SAFE_PUSH_TEXT(ent->gr_passwd);
1c9eff1997-09-07Per Hedbor  push_int(ent->gr_gid); { char **cp = ent->gr_mem; int i=0;
7fb1492012-01-25Henrik Grubbström (Grubba)  while(cp && cp[i]) push_text(cp[i++]);
1c9eff1997-09-07Per Hedbor  f_aggregate(i); } f_aggregate(4); }
1f606b1997-01-28Henrik Grubbström (Grubba) #endif
51dc981998-03-03Martin Stjernholm  #ifdef HAVE_GETGRGID
a0f8352001-11-13Martin Nilsson /*! @decl array(int|string|array(string)) getgrgid(int gid) *!
13a80a2002-03-02Martin Nilsson  *! Get the group entry for the group with the id @[gid] using the systemfunction
9ad7f52002-03-02Marcus Agehall  *! @tt{getgrid(3)@}.
26a0062002-03-01Marcus Agehall  *! *! @param gid *! The id of the group *! *! @returns *! An array with the information about the group *! @array *! @elem string 0 *! Group name *! @elem string 1 *! Group password (encrypted) *! @elem int 2 *! ID of the group *! @elem array 3.. *! Array with UIDs of group members *! @endarray *! *! @seealso *! @[getgrent()] *! @[getgrnam()]
a0f8352001-11-13Martin Nilsson  */
1c9eff1997-09-07Per Hedbor void f_getgrgid(INT32 args) {
65a5492000-08-10Per Hedbor  INT_TYPE gid;
1c9eff1997-09-07Per Hedbor  struct group *foo;
391ac52018-08-05Martin Nilsson  get_all_args(NULL, args, "%i", &gid);
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
1c9eff1997-09-07Per Hedbor  foo = getgrgid( gid );
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
1c9eff1997-09-07Per Hedbor  pop_n_elems( args ); push_grent( foo );
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
1c9eff1997-09-07Per Hedbor }
51dc981998-03-03Martin Stjernholm #endif /* HAVE_GETGRGID */
1c9eff1997-09-07Per Hedbor 
51dc981998-03-03Martin Stjernholm #ifdef HAVE_GETGRNAM
a0f8352001-11-13Martin Nilsson /*! @decl array(int|string|array(string)) getgrnam(string str)
13a80a2002-03-02Martin Nilsson  *! Get the group entry for the group with the name @[str] using the
9ad7f52002-03-02Marcus Agehall  *! systemfunction @tt{getgrnam(3)@}.
26a0062002-03-01Marcus Agehall  *! *! @param str *! The name of the group *! *! @returns *! An array with the information about the group *! @array *! @elem string 0 *! Group name *! @elem string 1 *! Group password (encrypted) *! @elem int 2 *! ID of the group *! @elem array 3.. *! Array with UIDs of group members *! @endarray
a0f8352001-11-13Martin Nilsson  *!
26a0062002-03-01Marcus Agehall  *! @seealso *! @[getgrent()] *! @[getgrgid()]
a0f8352001-11-13Martin Nilsson  */
1c9eff1997-09-07Per Hedbor void f_getgrnam(INT32 args) { char *str; struct group *foo;
391ac52018-08-05Martin Nilsson  get_all_args(NULL, args, "%s", &str);
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
1c9eff1997-09-07Per Hedbor  foo = getgrnam( str );
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
1c9eff1997-09-07Per Hedbor  pop_n_elems( args ); push_grent( foo );
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
1c9eff1997-09-07Per Hedbor }
51dc981998-03-03Martin Stjernholm #endif /* HAVE_GETGRNAM */
1c9eff1997-09-07Per Hedbor 
51dc981998-03-03Martin Stjernholm #ifdef HAVE_GETPWNAM
a0f8352001-11-13Martin Nilsson /*! @decl array(int|string) getpwnam(string str) *!
13a80a2002-03-02Martin Nilsson  *! Get the user entry for login @[str] using the systemfunction @tt{getpwnam(3)@}.
26a0062002-03-01Marcus Agehall  *! *! @param str *! The login name of the user whos userrecord is requested. *! *! @returns *! An array with the information about the user *! @array *! @elem string 0 *! Users username (loginname) *! @elem string 1 *! User password (encrypted) *! @elem int 2 *! Users ID *! @elem int 3 *! Users primary group ID *! @elem string 4 *! Users real name an possibly some other info *! @elem string 5 *! Users home directory *! @elem string 6 *! Users shell *! @endarray *! *! @seealso *! @[getpwuid()] *! @[getpwent()]
a0f8352001-11-13Martin Nilsson  */
1f606b1997-01-28Henrik Grubbström (Grubba) void f_getpwnam(INT32 args) { char *str; struct passwd *foo;
391ac52018-08-05Martin Nilsson  get_all_args(NULL, args, "%s", &str);
1f606b1997-01-28Henrik Grubbström (Grubba) 
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
1f606b1997-01-28Henrik Grubbström (Grubba)  foo = getpwnam(str);
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
1f606b1997-01-28Henrik Grubbström (Grubba)  pop_n_elems(args); push_pwent(foo);
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
1f606b1997-01-28Henrik Grubbström (Grubba) }
51dc981998-03-03Martin Stjernholm #endif /* HAVE_GETPWNAM */
1f606b1997-01-28Henrik Grubbström (Grubba) 
51dc981998-03-03Martin Stjernholm #ifdef HAVE_GETPWUID
a0f8352001-11-13Martin Nilsson /*! @decl array(int|string) getpwuid(int uid) *!
13a80a2002-03-02Martin Nilsson  *! Get the user entry for UID @[uid] using the systemfunction @tt{getpwuid(3)@}.
26a0062002-03-01Marcus Agehall  *! *! @param uid *! The uid of the user whos userrecord is requested. *! *! @returns *! An array with the information about the user *! @array *! @elem string 0 *! Users username (loginname) *! @elem string 1 *! User password (encrypted) *! @elem int 2 *! Users ID *! @elem int 3 *! Users primary group ID *! @elem string 4 *! Users real name an possibly some other info *! @elem string 5 *! Users home directory *! @elem string 6 *! Users shell *! @endarray *! *! @seealso *! @[getpwnam()] *! @[getpwent()]
a0f8352001-11-13Martin Nilsson  */
1f606b1997-01-28Henrik Grubbström (Grubba) void f_getpwuid(INT32 args) {
65a5492000-08-10Per Hedbor  INT_TYPE uid;
1f606b1997-01-28Henrik Grubbström (Grubba)  struct passwd *foo;
26a0062002-03-01Marcus Agehall 
391ac52018-08-05Martin Nilsson  get_all_args(NULL, args, "%i", &uid);
1f606b1997-01-28Henrik Grubbström (Grubba) 
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
1f606b1997-01-28Henrik Grubbström (Grubba)  foo = getpwuid(uid);
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
1f606b1997-01-28Henrik Grubbström (Grubba)  pop_n_elems(args); push_pwent(foo);
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
1f606b1997-01-28Henrik Grubbström (Grubba) }
51dc981998-03-03Martin Stjernholm #endif /* HAVE_GETPWUID */
dce4542003-04-02Martin Nilsson /*! @module System */
18cd451997-07-10Henrik Grubbström (Grubba) #ifdef HAVE_SETPWENT
a0f8352001-11-13Martin Nilsson /*! @decl int setpwent() *!
13a80a2002-03-02Martin Nilsson  *! Resets the @[getpwent] function to the first entry in the passwd source
9ad7f52002-03-02Marcus Agehall  *! using the systemfunction @tt{setpwent(3)@}.
26a0062002-03-01Marcus Agehall  *! *! @returns
cbe8c92003-04-07Martin Nilsson  *! Always @expr{0@} (zero)
26a0062002-03-01Marcus Agehall  *! *! @seealso
dce4542003-04-02Martin Nilsson  *! @[get_all_users()]
26a0062002-03-01Marcus Agehall  *! @[getpwent()] *! @[endpwent()]
a0f8352001-11-13Martin Nilsson  */
1f606b1997-01-28Henrik Grubbström (Grubba) void f_setpwent(INT32 args) {
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
1f606b1997-01-28Henrik Grubbström (Grubba)  setpwent();
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
1f606b1997-01-28Henrik Grubbström (Grubba)  pop_n_elems(args); push_int(0);
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
1f606b1997-01-28Henrik Grubbström (Grubba) }
18cd451997-07-10Henrik Grubbström (Grubba) #endif /* HAVE_SETPWENT */
26a0062002-03-01Marcus Agehall 
51dc981998-03-03Martin Stjernholm #ifdef HAVE_ENDPWENT
a0f8352001-11-13Martin Nilsson /*! @decl int endpwent() *!
13a80a2002-03-02Martin Nilsson  *! Closes the passwd source opened by @[getpwent] function using the
9ad7f52002-03-02Marcus Agehall  *! systemfunction @tt{endpwent(3)@}.
26a0062002-03-01Marcus Agehall  *! *! @returns
cbe8c92003-04-07Martin Nilsson  *! Always @expr{0@} (zero)
26a0062002-03-01Marcus Agehall  *! *! @seealso
dce4542003-04-02Martin Nilsson  *! @[get_all_users()]
26a0062002-03-01Marcus Agehall  *! @[getpwent()] *! @[setpwent()]
a0f8352001-11-13Martin Nilsson  */
1f606b1997-01-28Henrik Grubbström (Grubba) void f_endpwent(INT32 args) {
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
1f606b1997-01-28Henrik Grubbström (Grubba)  endpwent();
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
1f606b1997-01-28Henrik Grubbström (Grubba)  pop_n_elems(args); push_int(0);
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
1f606b1997-01-28Henrik Grubbström (Grubba) }
51dc981998-03-03Martin Stjernholm #endif /* HAVE_ENDPWENT */
1f606b1997-01-28Henrik Grubbström (Grubba) 
51dc981998-03-03Martin Stjernholm #ifdef HAVE_GETPWENT
a0f8352001-11-13Martin Nilsson /*! @decl array(int|string) getpwent() *!
13a80a2002-03-02Martin Nilsson  *! When first called, the @[getpwent] function opens the passwd source
9ad7f52002-03-02Marcus Agehall  *! and returns the first record using the systemfunction @tt{getpwent(3)@}.
26a0062002-03-01Marcus Agehall  *! For each following call, it returns the next record until EOF. *!
13a80a2002-03-02Martin Nilsson  *! Call @[endpwent] when done using @[getpwent].
26a0062002-03-01Marcus Agehall  *! *! @returns *! An array with the information about the user *! @array *! @elem string 0 *! Users username (loginname) *! @elem string 1 *! User password (encrypted) *! @elem int 2 *! Users ID *! @elem int 3 *! Users primary group ID *! @elem string 4 *! Users real name an possibly some other info *! @elem string 5 *! Users home directory *! @elem string 6 *! Users shell *! @endarray *! *! 0 if EOF. *! *! @seealso
dce4542003-04-02Martin Nilsson  *! @[get_all_users()]
26a0062002-03-01Marcus Agehall  *! @[getpwnam()] *! @[getpwent()] *! @[setpwent()] *! @[endpwent()]
a0f8352001-11-13Martin Nilsson  */
1f606b1997-01-28Henrik Grubbström (Grubba) void f_getpwent(INT32 args) { struct passwd *foo; pop_n_elems(args);
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
1f606b1997-01-28Henrik Grubbström (Grubba)  foo = getpwent();
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
1f606b1997-01-28Henrik Grubbström (Grubba)  if(!foo) {
a6a1d41998-07-14Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
1f606b1997-01-28Henrik Grubbström (Grubba)  push_int(0); return; } push_pwent(foo);
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
1f606b1997-01-28Henrik Grubbström (Grubba) }
57ee3a1998-04-06Fredrik Hübinette (Hubbe) 
dce4542003-04-02Martin Nilsson /*! @endmodule */
a0f8352001-11-13Martin Nilsson /*! @decl array(array(int|string)) get_all_users() *!
26a0062002-03-01Marcus Agehall  *! Returns an array with all users in the system. *! *! @returns
13a80a2002-03-02Martin Nilsson  *! An array with arrays of userinfo as in @[getpwent].
26a0062002-03-01Marcus Agehall  *! *! @seealso *! @[getpwent()] *! @[getpwnam()] *! @[getpwuid()]
a0f8352001-11-13Martin Nilsson  */
57ee3a1998-04-06Fredrik Hübinette (Hubbe) void f_get_all_users(INT32 args) { struct array *a;
c946a11998-04-17Henrik Grubbström (Grubba) 
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  pop_n_elems(args);
9f46691998-04-20Henrik Grubbström (Grubba)  a = low_allocate_array(0, 10);
c946a11998-04-17Henrik Grubbström (Grubba) 
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
c946a11998-04-17Henrik Grubbström (Grubba) 
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  setpwent(); while(1) {
c946a11998-04-17Henrik Grubbström (Grubba)  struct passwd *pw;
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  pw=getpwent();
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
9f46691998-04-20Henrik Grubbström (Grubba) 
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  if(!pw) break;
c946a11998-04-17Henrik Grubbström (Grubba) 
9f46691998-04-20Henrik Grubbström (Grubba)  push_pwent(pw); a = append_array(a, sp-1); pop_stack();
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  } endpwent();
c946a11998-04-17Henrik Grubbström (Grubba) 
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
c946a11998-04-17Henrik Grubbström (Grubba) 
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  push_array(a); }
18cd451997-07-10Henrik Grubbström (Grubba) #endif /* HAVE_GETPWENT */
51dc981998-03-03Martin Stjernholm 
dce4542003-04-02Martin Nilsson /*! @module System */
51dc981998-03-03Martin Stjernholm #ifdef HAVE_SETGRENT
a0f8352001-11-13Martin Nilsson /*! @decl int setgrent() *!
13a80a2002-03-02Martin Nilsson  *! Rewinds the @[getgrent] pointer to the first entry
26a0062002-03-01Marcus Agehall  *! *! @seealso
dce4542003-04-02Martin Nilsson  *! @[get_all_groups()]
26a0062002-03-01Marcus Agehall  *! @[getgrent()] *! @[endgrent()]
a0f8352001-11-13Martin Nilsson  */
51dc981998-03-03Martin Stjernholm void f_setgrent(INT32 args) {
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
51dc981998-03-03Martin Stjernholm  setgrent();
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
51dc981998-03-03Martin Stjernholm  pop_n_elems(args); push_int(0); } #endif /* HAVE_SETGRENT */ #ifdef HAVE_ENDGRENT
a0f8352001-11-13Martin Nilsson /*! @decl int endgrent() *!
13a80a2002-03-02Martin Nilsson  *! Closes the /etc/groups file after using the @[getgrent] function.
26a0062002-03-01Marcus Agehall  *! *! @seealso
dce4542003-04-02Martin Nilsson  *! @[get_all_groups()]
26a0062002-03-01Marcus Agehall  *! @[getgrent()] *! @[setgrent()]
a0f8352001-11-13Martin Nilsson  */
51dc981998-03-03Martin Stjernholm void f_endgrent(INT32 args) {
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
51dc981998-03-03Martin Stjernholm  endgrent();
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
51dc981998-03-03Martin Stjernholm  pop_n_elems(args); push_int(0); } #endif /* HAVE_ENDGRENT */ #ifdef HAVE_GETGRENT
a0f8352001-11-13Martin Nilsson /*! @decl array(int|string|array(string)) getgrent() *!
26a0062002-03-01Marcus Agehall  *! Get a group entry from /etc/groups file.
13a80a2002-03-02Martin Nilsson  *! @[getgrent] interates thru the groups source and returns
9ad7f52002-03-02Marcus Agehall  *! one entry per call using the systemfunction @tt{getgrent(3)@}.
26a0062002-03-01Marcus Agehall  *!
13a80a2002-03-02Martin Nilsson  *! Always call @[endgrent] when done using @[getgrent]!
26a0062002-03-01Marcus Agehall  *! *! @returns *! An array with the information about the group *! @array *! @elem string 0 *! Group name *! @elem string 1 *! Group password (encrypted) *! @elem int 2 *! ID of the group *! @elem array 3.. *! Array with UIDs of group members *! @endarray *! *! @seealso
dce4542003-04-02Martin Nilsson  *! @[get_all_groups()]
26a0062002-03-01Marcus Agehall  *! @[getgrnam()] *! @[getgrgid()]
a0f8352001-11-13Martin Nilsson  */
51dc981998-03-03Martin Stjernholm void f_getgrent(INT32 args) { struct group *foo; pop_n_elems(args);
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
51dc981998-03-03Martin Stjernholm  foo = getgrent();
ef1e931998-03-26Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
51dc981998-03-03Martin Stjernholm  if(!foo) {
71a05a1998-11-09Martin Stjernholm  UNLOCK_IMUTEX(&password_protection_mutex);
51dc981998-03-03Martin Stjernholm  push_int(0); return; } push_grent(foo);
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
57ee3a1998-04-06Fredrik Hübinette (Hubbe) }
dce4542003-04-02Martin Nilsson /*! @endmodule */
a0f8352001-11-13Martin Nilsson /*! @decl array(array(int|string|array(string))) get_all_groups() *!
9ad7f52002-03-02Marcus Agehall  *! Returns an array of arrays with all groups in the system groups source.
26a0062002-03-01Marcus Agehall  *! Each element in the returned array has the same structure as in
13a80a2002-03-02Martin Nilsson  *! @[getgrent] function.
26a0062002-03-01Marcus Agehall  *!
9ad7f52002-03-02Marcus Agehall  *! @note *! The groups source is system dependant. Refer to your system manuals for information *! about how to set the source. *!
26a0062002-03-01Marcus Agehall  *! @returns *! @array *! @elem array(int|string|array(string)) 0.. *! Array with info about the group *! @endarray *! *! @seealso *! @[getgrent()]
a0f8352001-11-13Martin Nilsson  */
57ee3a1998-04-06Fredrik Hübinette (Hubbe) void f_get_all_groups(INT32 args) { struct array *a;
c946a11998-04-17Henrik Grubbström (Grubba) 
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  pop_n_elems(args);
c946a11998-04-17Henrik Grubbström (Grubba) 
9f46691998-04-20Henrik Grubbström (Grubba)  a = low_allocate_array(0, 10);
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
c946a11998-04-17Henrik Grubbström (Grubba) 
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  setgrent(); while(1) {
c946a11998-04-17Henrik Grubbström (Grubba)  struct group *gr;
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  gr=getgrent();
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
c946a11998-04-17Henrik Grubbström (Grubba) 
9f46691998-04-20Henrik Grubbström (Grubba)  if(!gr) break;
c946a11998-04-17Henrik Grubbström (Grubba) 
9f46691998-04-20Henrik Grubbström (Grubba)  push_grent(gr); a = append_array(a, sp-1); pop_stack();
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  } endgrent();
c946a11998-04-17Henrik Grubbström (Grubba) 
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
c946a11998-04-17Henrik Grubbström (Grubba) 
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  push_array(a);
51dc981998-03-03Martin Stjernholm }
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  #ifdef HAVE_GETPWNAM
a0f8352001-11-13Martin Nilsson /*! @decl array(int) get_groups_for_user(int|string user) *!
26a0062002-03-01Marcus Agehall  *! Gets all groups which a given user is a member of. *! *! @param user *! UID or loginname of the user *! *! @returns *! @array *! @elem array 0.. *! Information about all the users groups *! @endarray *! *! @seealso *! @[get_all_groups()] *! @[getgrgid()] *! @[getgrnam()] *! @[getpwuid()] *! @[getpwnam()]
a0f8352001-11-13Martin Nilsson  */
57ee3a1998-04-06Fredrik Hübinette (Hubbe) void f_get_groups_for_user(INT32 arg) {
8269b62000-08-07Henrik Grubbström (Grubba)  struct group *gr = NULL;
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  struct passwd *pw; struct array *a;
973fb71998-07-22Henrik Grubbström (Grubba)  char *user = NULL; /* Keep compiler happy */
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  ONERROR err; int base_gid;
d103422018-08-05Martin Nilsson  check_all_args(NULL,arg,BIT_INT | BIT_STRING, 0);
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  pop_n_elems(arg-1); a=low_allocate_array(0,10);
017b572011-10-28Henrik Grubbström (Grubba)  if(TYPEOF(sp[-1]) == T_INT)
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  { int uid=sp[-1].u.integer;
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  pw=getpwuid(uid);
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
3889db1998-04-17Henrik Grubbström (Grubba) 
c7f2a71998-07-17Fredrik Hübinette (Hubbe)  if(pw) {
017b572011-10-28Henrik Grubbström (Grubba)  SET_SVAL(sp[-1], T_STRING, 0, string, make_shared_string(pw->pw_name));
c7f2a71998-07-17Fredrik Hübinette (Hubbe)  user=sp[-1].u.string->str; }
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  }else{ user=sp[-1].u.string->str;
a91ca01998-07-10Henrik Grubbström (Grubba)  LOCK_IMUTEX(&password_protection_mutex);
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  pw=getpwnam(user);
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID();
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  }
3889db1998-04-17Henrik Grubbström (Grubba)  /* NOTE: password_protection_mutex is still locked here. */
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  if(!pw) {
a6a1d41998-07-14Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  pop_stack(); push_int(0); return; } push_int(base_gid=pw->pw_gid); a=append_array(a,sp-1); pop_stack(); setgrent();
8269b62000-08-07Henrik Grubbström (Grubba)  do {
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  int e;
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_ALLOW_UID();
57ee3a1998-04-06Fredrik Hübinette (Hubbe) 
8269b62000-08-07Henrik Grubbström (Grubba)  while ((gr = getgrent()))
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  {
8269b62000-08-07Henrik Grubbström (Grubba)  if((size_t)gr->gr_gid == (size_t)base_gid) continue;
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  for(e=0;gr->gr_mem[e];e++) if(!strcmp(gr->gr_mem[e],user)) break; if(gr->gr_mem[e]) break; }
053eeb1998-07-09Henrik Grubbström (Grubba)  THREADS_DISALLOW_UID(); /* Is there a risk of deadlock here? */
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  if(!gr) break; push_int(gr->gr_gid); a=append_array(a,sp-1); pop_stack();
8269b62000-08-07Henrik Grubbström (Grubba)  } while(gr);
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  endgrent();
a91ca01998-07-10Henrik Grubbström (Grubba)  UNLOCK_IMUTEX(&password_protection_mutex);
26a0062002-03-01Marcus Agehall 
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  pop_stack(); push_array(a); } #endif /* HAVE_GETPWENT */
51dc981998-03-03Martin Stjernholm #endif /* HAVE_GETGRENT */
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  void init_passwd(void) {
a91ca01998-07-10Henrik Grubbström (Grubba)  init_interleave_mutex(&password_protection_mutex);
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  /* * From passwords.c */ #ifdef HAVE_GETPWNAM
26a0062002-03-01Marcus Agehall 
45ee5d1999-02-10Fredrik Hübinette (Hubbe) /* function(string:array(int|string)) */
26a0062002-03-01Marcus Agehall  ADD_EFUN("getpwnam", f_getpwnam,tFunc(tStr,tArr(tOr(tInt,tStr))),
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  OPT_SIDE_EFFECT |OPT_EXTERNAL_DEPEND); #endif #ifdef HAVE_GETPWUID
26a0062002-03-01Marcus Agehall 
45ee5d1999-02-10Fredrik Hübinette (Hubbe) /* function(int:array(int|string)) */ ADD_EFUN("getpwuid", f_getpwuid,tFunc(tInt,tArr(tOr(tInt,tStr))), OPT_SIDE_EFFECT |OPT_EXTERNAL_DEPEND);
57ee3a1998-04-06Fredrik Hübinette (Hubbe) #endif #ifdef HAVE_GETGRNAM
26a0062002-03-01Marcus Agehall 
45ee5d1999-02-10Fredrik Hübinette (Hubbe) /* function(string:array(int|string|array(string))) */ ADD_EFUN("getgrnam", f_getgrnam,tFunc(tStr,tArr(tOr3(tInt,tStr,tArr(tStr)))),
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  OPT_SIDE_EFFECT |OPT_EXTERNAL_DEPEND); #endif #ifdef HAVE_GETGRGID
26a0062002-03-01Marcus Agehall 
45ee5d1999-02-10Fredrik Hübinette (Hubbe) /* function(int:array(int|string|array(string))) */ ADD_EFUN("getgrgid", f_getgrgid,tFunc(tInt,tArr(tOr3(tInt,tStr,tArr(tStr)))), OPT_SIDE_EFFECT |OPT_EXTERNAL_DEPEND);
57ee3a1998-04-06Fredrik Hübinette (Hubbe) #endif #ifdef HAVE_GETPWENT
26a0062002-03-01Marcus Agehall 
e05b9f2003-04-02Martin Nilsson  /* function(void:array(int|string)) */ ADD_FUNCTION("getpwent", f_getpwent,tFunc(tVoid,tArr(tOr(tInt,tStr))), OPT_SIDE_EFFECT |OPT_EXTERNAL_DEPEND);
26a0062002-03-01Marcus Agehall 
e05b9f2003-04-02Martin Nilsson  /* function(void:array(array(int|string))) */
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  ADD_EFUN("get_all_users", f_get_all_users,tFunc(tVoid,tArr(tArr(tOr(tInt,tStr)))),
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  OPT_SIDE_EFFECT | OPT_EXTERNAL_DEPEND); #endif #ifdef HAVE_ENDPWENT
26a0062002-03-01Marcus Agehall 
e05b9f2003-04-02Martin Nilsson  /* function(void:int) */ ADD_FUNCTION("endpwent", f_endpwent,tFunc(tVoid,tInt), OPT_SIDE_EFFECT | OPT_EXTERNAL_DEPEND);
57ee3a1998-04-06Fredrik Hübinette (Hubbe) #endif #ifdef HAVE_SETPWENT
26a0062002-03-01Marcus Agehall 
e05b9f2003-04-02Martin Nilsson  /* function(void:int) */ ADD_FUNCTION("setpwent", f_setpwent,tFunc(tVoid,tInt), OPT_SIDE_EFFECT | OPT_EXTERNAL_DEPEND);
57ee3a1998-04-06Fredrik Hübinette (Hubbe) #endif #ifdef HAVE_GETGRENT
26a0062002-03-01Marcus Agehall 
e05b9f2003-04-02Martin Nilsson  /* function(void:array(int|string|array(string))) */ ADD_FUNCTION("getgrent", f_getgrent, tFunc(tVoid,tArr(tOr3(tInt,tStr,tArr(tStr)))), OPT_SIDE_EFFECT | OPT_EXTERNAL_DEPEND);
26a0062002-03-01Marcus Agehall 
e05b9f2003-04-02Martin Nilsson  /* function(void:array(array(int|string|array(string)))) */
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  ADD_EFUN("get_all_groups", f_get_all_groups,tFunc(tVoid,tArr(tArr(tOr3(tInt,tStr,tArr(tStr))))),
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  OPT_SIDE_EFFECT | OPT_EXTERNAL_DEPEND); #ifdef HAVE_GETPWENT
26a0062002-03-01Marcus Agehall 
45ee5d1999-02-10Fredrik Hübinette (Hubbe) /* function(int|string:array(int)) */ ADD_EFUN("get_groups_for_user", f_get_groups_for_user,tFunc(tOr(tInt,tStr),tArr(tInt)),
57ee3a1998-04-06Fredrik Hübinette (Hubbe)  OPT_SIDE_EFFECT | OPT_EXTERNAL_DEPEND); #endif /* HAVE_GETPWENT */ #endif #ifdef HAVE_ENDGRENT
26a0062002-03-01Marcus Agehall 
e05b9f2003-04-02Martin Nilsson  /* function(void:int) */ ADD_FUNCTION("endgrent", f_endgrent,tFunc(tVoid,tInt), OPT_SIDE_EFFECT | OPT_EXTERNAL_DEPEND);
57ee3a1998-04-06Fredrik Hübinette (Hubbe) #endif #ifdef HAVE_SETGRENT
26a0062002-03-01Marcus Agehall 
e05b9f2003-04-02Martin Nilsson  /* function(void:int) */ ADD_FUNCTION("setgrent", f_setgrent,tFunc(tVoid,tInt), OPT_SIDE_EFFECT |OPT_EXTERNAL_DEPEND);
57ee3a1998-04-06Fredrik Hübinette (Hubbe) #endif }