aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunk@stsci.edu>2011-09-29 09:24:37 -0400
committerJoseph Hunkeler <jhunk@stsci.edu>2011-09-29 09:24:37 -0400
commit576efc2d32e6689f3b33bc71c50512716bf99a38 (patch)
treec3f822db7c36a51f109a048e14e54717e2623819
parentefbeacd741eb7b938401a1f50894158c279386b3 (diff)
downloadduser-576efc2d32e6689f3b33bc71c50512716bf99a38.tar.gz
Initial work on creating lists (properly), but it does not function yet.
-rw-r--r--src/mklist.c126
1 files changed, 124 insertions, 2 deletions
diff --git a/src/mklist.c b/src/mklist.c
index c445b86..6da5cfa 100644
--- a/src/mklist.c
+++ b/src/mklist.c
@@ -4,12 +4,12 @@
*
* This file is part of duser.
*
-* duser is free software: you can redistribute it and/or modify
+* duser is free software: you can redifilenameibute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
-* duser is distributed in the hope that it will be useful,
+* duser is difilenameibuted in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
@@ -18,4 +18,126 @@
* along with duser. If not, see <http://www.gnu.org/licenses/>.
**/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <errno.h>
+#include <libgen.h>
+#include <pcre.h>
+#include "duser.h"
+char* str_replace(char* str, char* old, char* new)
+{
+ static char buffer[BUFSIZ];
+ char *p;
+ if(!(p = strstr(str, old)))
+ return NULL;
+
+ strncpy(buffer, str, p-str);
+ buffer[p-str] = 0;
+ sprintf(buffer+(p-str), "%s%s", new, p+strlen(old));
+
+ return buffer;
+}
+
+char* getfile(const char* filename)
+{
+ int bytes = 0;
+ static char buffer[BUFSIZ];
+ FILE* fp = NULL;
+ if((fp = fopen(filename, "r")) == NULL)
+ {
+ fprintf(stderr, "%s: fopen: %s\n", SELF, strerror(errno));
+ return NULL;
+ }
+
+ memset(buffer, 0, strlen(buffer));
+ if((bytes = fread(buffer, BUFSIZ, 1, fp)) < 0)
+ {
+ fprintf(stderr, "%s: fread: %s\n", SELF, strerror(errno));
+ exit(1);
+ }
+ buffer[strlen(buffer)] = 0;
+ fclose(fp);
+
+ return buffer;
+}
+
+static char* mailfmt = "mail -s '%s' -r %s %s";
+int mailer(const char* subj, const char* from, const char* to, const char* mesg)
+{
+ char mail[BUFSIZ];
+ if((snprintf(mail, BUFSIZ, mailfmt, subj, from, to)) < 1 )
+ {
+ fprintf(stderr, "%s: Failed to generate mail string.\n", SELF);
+ return -1;
+ }
+
+ FILE *pipe;
+ if((pipe = popen(mail, "w")) == NULL)
+ {
+ fprintf(stderr, "%s: Unable to open pipe.\n", SELF);
+ return -1;
+ }
+ fputs(mesg, pipe);
+ pclose(pipe);
+ return 0;
+}
+
+int mklist(char* filename)
+{
+ uid_t uid;
+ struct passwd *pwd;
+ char tmpname[strlen(filename)+1];
+// char* list = basename(filename);
+ int status = 0;
+ uid = getuid();
+ pwd = getpwuid(uid);
+
+ if((status = access(filename, F_OK)) == 0)
+ {
+ COM(SELF, "FATAL: %s: '%s' already exists\n", basename(filename), strerror(errno));
+ fprintf(stderr, "'%s' already exists\n", basename(filename));
+ return -1;
+ }
+
+ strncpy(tmpname, filename, strlen(filename));
+ strncat(tmpname, ".info", strlen(filename));
+ if((status = touch(tmpname, 0644)) != 0)
+ {
+ COM(SELF, "FATAL: %s: %s: %s\n", tmpname, strerror(errno));
+ fprintf(stderr, "FATAL: %s: %s: %s\n", SELF, tmpname, strerror(errno));
+ }
+
+ char* tmptpl;
+// char* tpl;
+ if((tmptpl = getfile("/usr/local/etc/duser/tpl/aliases")) == NULL)
+ {
+ fprintf(stderr, "%s: %s\n", SELF, strerror(errno));
+ exit(1);
+ }
+
+ pcre *re;
+ const int OVECOUNT = 30;
+ const char* error;
+ int rc;
+ int erroffset;
+ int ovector[OVECOUNT];
+
+ if((re = pcre_compile("%s|list", 0, &error, &erroffset, NULL)) == NULL)
+ {
+ fprintf(stderr, "%s: PCRE compilation failed at offset %d: %s\n", SELF, erroffset, error);
+ return -1;
+ }
+
+ if((rc = pcre_exec(re, NULL, tmptpl, strlen(tmptpl), 0, 0, ovector, OVECOUNT)) < 0)
+ {
+ fprintf(stderr, "%s: PCRE match failed '%d'\n", SELF, rc);
+ }
+
+ printf("%s\n", (tmptpl + ovector[1]));
+ return 0;
+}