nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

qmail-getpw.c (1916B)


      1 #include <sys/types.h>
      2 #include <sys/stat.h>
      3 #include <pwd.h>
      4 #include "readwrite.h"
      5 #include "substdio.h"
      6 #include "subfd.h"
      7 #include "error.h"
      8 #include "exit.h"
      9 #include "byte.h"
     10 #include "str.h"
     11 #include "case.h"
     12 #include "fmt.h"
     13 #include "auto_users.h"
     14 #include "auto_break.h"
     15 #include "qlx.h"
     16 
     17 #define GETPW_USERLEN 32
     18 
     19 char *local;
     20 struct passwd *pw;
     21 char *dash;
     22 char *extension;
     23 
     24 int userext()
     25 {
     26   char username[GETPW_USERLEN];
     27   struct stat st;
     28 
     29   extension = local + str_len(local);
     30   for (;;) {
     31     if (extension - local < sizeof(username))
     32       if (!*extension || (*extension == *auto_break)) {
     33 	byte_copy(username,extension - local,local);
     34 	username[extension - local] = 0;
     35 	case_lowers(username);
     36 	errno = 0;
     37 	pw = getpwnam(username);
     38 	if (errno == error_txtbsy) _exit(QLX_SYS);
     39 	if (pw)
     40 	  if (pw->pw_uid) {
     41 	    if (stat(pw->pw_dir,&st) == 0) {
     42 	      if (st.st_uid == pw->pw_uid) {
     43 		dash = "";
     44 		if (*extension) { ++extension; dash = "-"; }
     45 		return 1;
     46 	      }
     47 	    }
     48 	    else
     49 	      if (error_temp(errno)) _exit(QLX_NFS);
     50 	  }
     51       }
     52     if (extension == local) return 0;
     53     --extension;
     54   }
     55 }
     56 
     57 char num[FMT_ULONG];
     58 
     59 int main(int argc, char **argv)
     60 {
     61   if (argc == 1) return 100;
     62   local = argv[1];
     63 
     64   if (!userext()) {
     65     extension = local;
     66     dash = "-";
     67     pw = getpwnam(auto_usera);
     68   }
     69 
     70   if (!pw) return QLX_NOALIAS;
     71 
     72   substdio_puts(subfdoutsmall,pw->pw_name);
     73   substdio_put(subfdoutsmall,"",1);
     74   substdio_put(subfdoutsmall,num,fmt_ulong(num,(long) pw->pw_uid));
     75   substdio_put(subfdoutsmall,"",1);
     76   substdio_put(subfdoutsmall,num,fmt_ulong(num,(long) pw->pw_gid));
     77   substdio_put(subfdoutsmall,"",1);
     78   substdio_puts(subfdoutsmall,pw->pw_dir);
     79   substdio_put(subfdoutsmall,"",1);
     80   substdio_puts(subfdoutsmall,dash);
     81   substdio_put(subfdoutsmall,"",1);
     82   substdio_puts(subfdoutsmall,extension);
     83   substdio_put(subfdoutsmall,"",1);
     84   substdio_flush(subfdoutsmall);
     85 
     86   return 0;
     87 }