diff options
author | Nalin Dahyabhai <nalin.dahyabhai@pobox.com> | 2009-04-01 19:50:30 -0400 |
---|---|---|
committer | Nalin Dahyabhai <nalin.dahyabhai@pobox.com> | 2009-04-01 19:50:30 -0400 |
commit | 4ef0f586a5d8d4bc4296ecb5aca57a60aa30cc50 (patch) | |
tree | b4e40650f73567ee520b33d8b0e651006e3497be | |
parent | 26036576cc67ee3552ed3ee5648f33c07b811254 (diff) | |
download | credmonger-4ef0f586a5d8d4bc4296ecb5aca57a60aa30cc50.tar.gz credmonger-4ef0f586a5d8d4bc4296ecb5aca57a60aa30cc50.tar.xz credmonger-4ef0f586a5d8d4bc4296ecb5aca57a60aa30cc50.zip |
- prepare for a pile of config files
-rw-r--r-- | src/credmonger.c | 248 |
1 files changed, 128 insertions, 120 deletions
diff --git a/src/credmonger.c b/src/credmonger.c index c4d8fbc..02ee37d 100644 --- a/src/credmonger.c +++ b/src/credmonger.c @@ -47,137 +47,142 @@ entries_read(void) list = NULL; n_entries = 0; - fp = fopen(CONFIG_FILE, "r"); - if (fp != NULL) { - while (fgets(buf, sizeof(buf), fp) != NULL) { - /* Trim end-of-line characters. */ - buf[strcspn(buf, "\r\n")] = '\0'; - /* Skip comments. */ - if (strchr("#:", buf[0]) != NULL) { - continue; - } - /* Trim end-of-line characters. */ - uids = buf; - /* Skip ahead to the keytab name and close the user - * name. */ - keytab = strchr(uids, ':'); - if (keytab == NULL) { - fprintf(stderr, "No keytab name field.\n"); - continue; - } - *keytab++ = '\0'; - /* Skip ahead to the client principal name and close - * the keytab name. */ - principal = strchr(keytab, ':'); - if (principal == NULL) { - fprintf(stderr, "No principal name field.\n"); - continue; - } - *principal++ = '\0'; - /* Skip ahead to the ccache name pattern and close the - * principal name. */ - fccache_pattern = strchr(principal, ':'); - if (fccache_pattern != NULL) { - *fccache_pattern++ = '\0'; - /* Close the principal name. */ - p = strchr(fccache_pattern, ':'); - if (p != NULL) { - *p++ = '\0'; + + fp = fopen(CONFIG_FILE, "r"); + if (fp != NULL) { + while (fgets(buf, sizeof(buf), fp) != NULL) { + /* Trim end-of-line characters. */ + buf[strcspn(buf, "\r\n")] = '\0'; + /* Skip comments. */ + if (strchr("#:", buf[0]) != NULL) { + continue; } - } - /* Now figure out the UID/GID. */ - uid = (uid_t) strtol(uids, &p, 10); - if ((p == NULL) || (*p != '\0')) { - /* Not a number, so treat it as a user name. */ - pwd = getpwnam(uids); - if (pwd != NULL) { - uid = pwd->pw_uid; - gid = pwd->pw_gid; - } else { + /* Trim end-of-line characters. */ + uids = buf; + /* Skip ahead to the keytab name and close the + * user name. */ + keytab = strchr(uids, ':'); + if (keytab == NULL) { fprintf(stderr, - "Unknown user \"%s\".\n", uids); + "No keytab name field.\n"); continue; } - } else { - /* Treat it as a number. */ - pwd = getpwuid(uid); - if (pwd != NULL) { - gid = pwd->pw_gid; - } else { + *keytab++ = '\0'; + /* Skip ahead to the client principal name and + * close the keytab name. */ + principal = strchr(keytab, ':'); + if (principal == NULL) { fprintf(stderr, - "Unknown user %lu.\n", - (unsigned long) uid); + "No principal name field.\n"); continue; } - } - /* Now go back and figure out the ccache name pattern. - * If it's empty, assume that we want a "session" file - * in the temporary directory. */ - if ((fccache_pattern == NULL) || - (strlen(fccache_pattern) == 0)) { - snprintf(fccache, sizeof(fccache), - "FILE:%s/krb5cc_%lu_XXXXXX", - getenv("TMPDIR") ?: "/tmp", - (unsigned long) uid); - } else { - /* If the user supplied us with a typed ccache - * name, just use it as-is. */ - if (strncmp(fccache_pattern, FCC_PREFIX, - strlen(FCC_PREFIX)) == 0) { - snprintf(fccache, sizeof(fccache), - "%s", fccache_pattern); + *principal++ = '\0'; + /* Skip ahead to the ccache name pattern and + * close the principal name. */ + fccache_pattern = strchr(principal, ':'); + if (fccache_pattern != NULL) { + *fccache_pattern++ = '\0'; + /* Close the principal name. */ + p = strchr(fccache_pattern, ':'); + if (p != NULL) { + *p++ = '\0'; + } + } + /* Now figure out the UID/GID. */ + uid = (uid_t) strtol(uids, &p, 10); + if ((p == NULL) || (*p != '\0')) { + /* Not a number, so treat it as a user + * name. */ + pwd = getpwnam(uids); + if (pwd != NULL) { + uid = pwd->pw_uid; + gid = pwd->pw_gid; + } else { + fprintf(stderr, + "Unknown user " + "\"%s\".\n", uids); + continue; + } } else { - /* Mark it as a file-based ccache, and - * take the filename as-is. */ + /* Treat it as a number. */ + pwd = getpwuid(uid); + if (pwd != NULL) { + gid = pwd->pw_gid; + } else { + fprintf(stderr, + "Unknown user %lu.\n", + (unsigned long) uid); + continue; + } + } + /* Now go back and figure out the ccache name + * pattern. If it's empty, assume that we want + * a unique file in the temporary directory. */ + if ((fccache_pattern == NULL) || + (strlen(fccache_pattern) == 0)) { snprintf(fccache, sizeof(fccache), - "FILE:%s", fccache_pattern); + "FILE:%s/krb5cc_%lu_XXXXXX", + getenv("TMPDIR") ?: "/tmp", + (unsigned long) uid); + } else { + /* If the user supplied us with a typed + * ccache name, just use it as-is. */ + if (strncmp(fccache_pattern, FCC_PREFIX, + strlen(FCC_PREFIX)) == 0) { + snprintf(fccache, sizeof(fccache), + "%s", fccache_pattern); + } else { + /* Mark it as a file-based + * ccache, and take the + * filename as-is. */ + snprintf(fccache, + sizeof(fccache), + "FILE:%s", + fccache_pattern); + } + } + fccache_pattern = fccache; + /* Make space for this entry in the list. */ + tmp = realloc(list, + sizeof(*list) * (n_entries + 2)); + if (tmp == NULL) { + fprintf(stderr, "Out of memory.\n"); + break; + } + list = tmp; + /* Allocate this entry. */ + list[n_entries] = malloc(sizeof(**list)); + if (list[n_entries] == NULL) { + fprintf(stderr, "Out of memory.\n"); + break; + } + entry = list[n_entries]; + /* NULL-terminate the list. */ + n_entries++; + list[n_entries] = NULL; + /* Initialize this entry. */ + memset(entry, 0, sizeof(*entry)); + entry->uid = uid; + entry->gid = gid; + entry->keytab = strdup(keytab); + if (entry->keytab == NULL) { + fprintf(stderr, "Out of memory.\n"); + break; + } + entry->principal_name = strdup(principal); + if (entry->principal_name == NULL) { + fprintf(stderr, "Out of memory.\n"); + break; + } + entry->fccache_pattern = + strdup(fccache_pattern); + if (entry->fccache_pattern == NULL) { + fprintf(stderr, "Out of memory.\n"); + break; } } - fccache_pattern = fccache; - /* Make space for this entry in the list. */ - tmp = realloc(list, sizeof(*list) * (n_entries + 2)); - if (tmp == NULL) { - fprintf(stderr, "Out of memory.\n"); - break; - } - list = tmp; - /* Allocate this entry. */ - list[n_entries] = malloc(sizeof(**list)); - if (list[n_entries] == NULL) { - fprintf(stderr, "Out of memory.\n"); - break; - } - entry = list[n_entries]; - /* NULL-terminate the list. */ - n_entries++; - list[n_entries] = NULL; - /* Initialize this entry. */ - memset(entry, 0, sizeof(*entry)); - entry->uid = uid; - entry->gid = gid; - entry->keytab = strdup(keytab); - if (entry->keytab == NULL) { - fprintf(stderr, "Out of memory.\n"); - break; - } - entry->principal_name = strdup(principal); - if (entry->principal_name == NULL) { - fprintf(stderr, "Out of memory.\n"); - break; - } - entry->fccache_pattern = strdup(fccache_pattern); - if (entry->fccache_pattern == NULL) { - fprintf(stderr, "Out of memory.\n"); - break; - } - fprintf(stderr, "%ld:%s,%s,%s\n", - (unsigned long) entry->uid, - entry->keytab, - entry->principal_name, - entry->fccache_pattern); + fclose(fp); } - fclose(fp); - } return list; } @@ -506,9 +511,12 @@ main(int argc, char **argv) } } + entries = entries_read(); + if (!nofork) { daemon(0, 0); } + fp = fopen(pidfile, "w"); if (fp != NULL) { fprintf(fp, "%lu\n", (long unsigned) (getpid())); @@ -519,7 +527,7 @@ main(int argc, char **argv) signal(SIGQUIT, setquit); /* Make sure we initialize the first time through. */ - howlong = -1; + howlong = 0; while (quit == 0) { /* Either (re-)initialize, or don't. */ if (howlong == 0) { |