summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjkar8572 <jkar8572>2005-06-29 08:47:42 +0000
committerjkar8572 <jkar8572>2005-06-29 08:47:42 +0000
commit108a8e7c027b7b51454d9ca10c695c87f1a76459 (patch)
treeb0aaaa4f33b10be0082c21d2c5f7699e112e4f2f
parent3175710dd6061c9f3df49e24728b93b4d8f29f89 (diff)
Implemented substitution of %i,%h,%d for user, host and domain in warnquota
messages (Jan Kara)
-rw-r--r--Changelog3
-rw-r--r--warnquota.c127
-rw-r--r--warnquota.conf14
3 files changed, 107 insertions, 37 deletions
diff --git a/Changelog b/Changelog
index 0273fde..795bb28 100644
--- a/Changelog
+++ b/Changelog
@@ -1,3 +1,6 @@
+Changes in quota-tools from 3.13 to 3.14
+* implemented nicer message formatting for warnquota(8) (Jan Kara)
+
Changes in quota-tools from 3.12 to 3.13
* fixed mistakes in several error messages, improved error reporting functions
(Dmitry V. Levin)
diff --git a/warnquota.c b/warnquota.c
index 1c7b405..25ea8f7 100644
--- a/warnquota.c
+++ b/warnquota.c
@@ -10,7 +10,7 @@
*
* Author: Marco van Wieringen <mvw@planets.elm.net>
*
- * Version: $Id: warnquota.c,v 1.22 2005/06/24 14:04:23 jkar8572 Exp $
+ * Version: $Id: warnquota.c,v 1.23 2005/06/29 08:47:44 jkar8572 Exp $
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -27,9 +27,10 @@
#include <ctype.h>
#include <signal.h>
#include <grp.h>
+#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
-#include <time.h>
+#include <sys/utsname.h>
#ifdef USE_LDAP_MAIL_LOOKUP
#include <ldap.h>
#endif
@@ -90,7 +91,7 @@ struct usage {
};
#ifdef USE_LDAP_MAIL_LOOKUP
-LDAP *ldapconn = NULL;
+static LDAP *ldapconn = NULL;
#endif
struct configparams {
@@ -137,14 +138,15 @@ struct adminstable {
char *adminname;
};
-int qtab_i = 0, fmt = -1, flags;
-char maildev[CNF_BUFFER];
-struct quota_handle *maildev_handle;
-char *configfile = WARNQUOTA_CONF, *quotatabfile = QUOTATAB, *adminsfile = ADMINSFILE;
+static int qtab_i = 0, fmt = -1, flags;
+static char maildev[CNF_BUFFER];
+static struct quota_handle *maildev_handle;
+static char *configfile = WARNQUOTA_CONF, *quotatabfile = QUOTATAB, *adminsfile = ADMINSFILE;
char *progname;
-quotatable_t *quotatable;
-int adminscnt, adminsalloc;
-struct adminstable *adminstable;
+static char *hostname, *domainname;
+static quotatable_t *quotatable;
+static int adminscnt, adminsalloc;
+static struct adminstable *adminstable;
/*
* Global pointers to list.
@@ -184,7 +186,7 @@ static int setup_ldap(struct configparams *config)
#endif
-struct offenderlist *add_offender(int type, int id, char *name)
+static struct offenderlist *add_offender(int type, int id, char *name)
{
struct offenderlist *offender;
char namebuf[MAXNAMELEN];
@@ -206,7 +208,7 @@ struct offenderlist *add_offender(int type, int id, char *name)
return offender;
}
-void add_offence(struct dquot *dquot, char *name)
+static void add_offence(struct dquot *dquot, char *name)
{
struct offenderlist *lptr;
struct usage *usage;
@@ -230,7 +232,7 @@ void add_offence(struct dquot *dquot, char *name)
lptr->usage = usage;
}
-int deliverable(struct dquot *dquot)
+static int deliverable(struct dquot *dquot)
{
time_t now;
struct dquot *mdquot;
@@ -259,7 +261,7 @@ int deliverable(struct dquot *dquot)
return 1;
}
-int check_offence(struct dquot *dquot, char *name)
+static int check_offence(struct dquot *dquot, char *name)
{
if ((dquot->dq_dqb.dqb_bsoftlimit && toqb(dquot->dq_dqb.dqb_curspace) >= dquot->dq_dqb.dqb_bsoftlimit)
|| (dquot->dq_dqb.dqb_isoftlimit && dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_isoftlimit)) {
@@ -269,7 +271,7 @@ int check_offence(struct dquot *dquot, char *name)
return 0;
}
-FILE *run_mailer(char *command)
+static FILE *run_mailer(char *command)
{
int pipefd[2];
FILE *f;
@@ -300,12 +302,12 @@ FILE *run_mailer(char *command)
}
}
-int admin_name_cmp(const void *key, const void *mem)
+static int admin_name_cmp(const void *key, const void *mem)
{
return strcmp(key, ((struct adminstable *)mem)->grpname);
}
-int should_cc(struct offenderlist *offender, struct configparams *config)
+static int should_cc(struct offenderlist *offender, struct configparams *config)
{
struct usage *lptr;
struct util_dqblk *dqb;
@@ -324,7 +326,35 @@ int should_cc(struct offenderlist *offender, struct configparams *config)
return 0;
}
-int mail_user(struct offenderlist *offender, struct configparams *config)
+/* Substitute %s and %i for 'name' and %h for hostname */
+static void format_print(FILE *fp, char *fmt, char *name)
+{
+ char *ch, *lastch = fmt;
+
+ for (ch = strchr(fmt, '%'); ch; lastch = ch+2, ch = strchr(ch+2, '%')) {
+ *ch = 0;
+ fputs(lastch, fp);
+ *ch = '%';
+ switch (*(ch+1)) {
+ case 's':
+ case 'i':
+ fputs(name, fp);
+ break;
+ case 'h':
+ fputs(hostname, fp);
+ break;
+ case 'd':
+ fputs(domainname, fp);
+ break;
+ case '%':
+ fputc('%', fp);
+ break;
+ }
+ }
+ fputs(lastch, fp);
+}
+
+static int mail_user(struct offenderlist *offender, struct configparams *config)
{
struct usage *lptr;
FILE *fp;
@@ -441,12 +471,12 @@ int mail_user(struct offenderlist *offender, struct configparams *config)
if (offender->offender_type == USRQUOTA)
if (config->user_message)
- fputs(config->user_message, fp);
+ format_print(fp, config->user_message, offender->offender_name);
else
fputs(DEF_USER_MESSAGE, fp);
else
if (config->group_message)
- fprintf(fp, config->group_message, offender->offender_name);
+ format_print(fp, config->group_message, offender->offender_name);
else
fprintf(fp, DEF_GROUP_MESSAGE, offender->offender_name);
@@ -491,12 +521,12 @@ int mail_user(struct offenderlist *offender, struct configparams *config)
if (offender->offender_type == USRQUOTA)
if (config->user_signature)
- fputs(config->user_signature, fp);
+ format_print(fp, config->user_signature, offender->offender_name);
else
fprintf(fp, DEF_USER_SIGNATURE, config->support, config->phone);
else
if (config->group_signature)
- fputs(config->group_signature, fp);
+ format_print(fp, config->group_signature, offender->offender_name);
else
fprintf(fp, DEF_GROUP_SIGNATURE, config->support, config->phone);
fclose(fp);
@@ -508,7 +538,7 @@ int mail_user(struct offenderlist *offender, struct configparams *config)
return 0;
}
-int mail_to_offenders(struct configparams *config)
+static int mail_to_offenders(struct configparams *config)
{
struct offenderlist *lptr;
int ret = 0;
@@ -524,7 +554,7 @@ int mail_to_offenders(struct configparams *config)
/*
* Wipe spaces, tabs, quotes and newlines from beginning and end of string
*/
-void stripstring(char **buff)
+static void stripstring(char **buff)
{
int i;
@@ -541,7 +571,7 @@ void stripstring(char **buff)
/*
* Substitute '|' with end of lines
*/
-void create_eoln(char *buf)
+static void create_eoln(char *buf)
{
char *colpos = buf;
@@ -552,7 +582,7 @@ void create_eoln(char *buf)
/*
* Read /etc/quotatab (description of devices for users)
*/
-int get_quotatable(void)
+static int get_quotatable(void)
{
FILE *fp;
char buffer[IOBUF_SIZE], *colpos, *devname, *devdesc;
@@ -603,11 +633,31 @@ int get_quotatable(void)
return 0;
}
+/* Check correctness of the given format */
+static void verify_format(char *fmt, char *varname)
+{
+ char *ch;
+
+ for (ch = strchr(fmt, '%'); ch; ch = strchr(ch+2, '%')) {
+ switch (*(ch+1)) {
+ case 's':
+ case 'i':
+ case 'h':
+ case 'd':
+ case '%':
+ continue;
+ default:
+ die(1, _("Incorrect format string for variable %s.\n\
+Unrecognized expression %%%c.\n"), varname, *(ch+1));
+ }
+ }
+}
+
/*
* Reads config parameters from configfile
* uses default values if errstr occurs
*/
-int readconfigfile(const char *filename, struct configparams *config)
+static int readconfigfile(const char *filename, struct configparams *config)
{
FILE *fp;
char buff[IOBUF_SIZE];
@@ -692,18 +742,22 @@ int readconfigfile(const char *filename, struct configparams *config)
} else if (!strcmp(var, "MESSAGE")) {
config->user_message = sstrdup(value);
create_eoln(config->user_message);
+ verify_format(config->user_message, "MESSAGE");
}
else if (!strcmp(var, "SIGNATURE")) {
config->user_signature = sstrdup(value);
create_eoln(config->user_signature);
+ verify_format(config->user_signature, "SIGNATURE");
}
else if (!strcmp(var, "GROUP_MESSAGE")) {
config->group_message = sstrdup(value);
create_eoln(config->group_message);
+ verify_format(config->group_message, "GROUP_MESSAGE");
}
else if (!strcmp(var, "GROUP_SIGNATURE")) {
config->group_signature = sstrdup(value);
create_eoln(config->group_signature);
+ verify_format(config->group_signature, "GROUP_SIGNATURE");
}
else if (!strcmp(var, "LDAP_MAIL")) {
if(strcasecmp(value, "true") == 0)
@@ -753,13 +807,13 @@ cc_parse_err:
return 0;
}
-int admin_cmp(const void *a1, const void *a2)
+static int admin_cmp(const void *a1, const void *a2)
{
return strcmp(((struct adminstable *)a1)->grpname, ((struct adminstable *)a2)->grpname);
}
/* Get administrators of the groups */
-int get_groupadmins(void)
+static int get_groupadmins(void)
{
FILE *f;
int line = 0;
@@ -815,7 +869,7 @@ int get_groupadmins(void)
return 0;
}
-struct quota_handle *find_handle_dev(char *dev, struct quota_handle **handles)
+static struct quota_handle *find_handle_dev(char *dev, struct quota_handle **handles)
{
int i;
@@ -823,7 +877,7 @@ struct quota_handle *find_handle_dev(char *dev, struct quota_handle **handles)
return handles[i];
}
-void warn_quota(void)
+static void warn_quota(void)
{
struct quota_handle **handles;
struct configparams config;
@@ -914,10 +968,21 @@ static void parse_options(int argcnt, char **argstr)
flags |= FL_USER;
}
+static void get_host_name(void)
+{
+ struct utsname uts;
+
+ if (uname(&uts))
+ die(1, _("Cannot get host name: %s\n"), strerror(errno));
+ hostname = uts.nodename;
+ domainname = uts.domainname;
+}
+
int main(int argc, char **argv)
{
gettexton();
progname = basename(argv[0]);
+ get_host_name();
parse_options(argc, argv);
init_kernel_interface();
diff --git a/warnquota.conf b/warnquota.conf
index 653f92c..d80f7ae 100644
--- a/warnquota.conf
+++ b/warnquota.conf
@@ -18,15 +18,17 @@ PHONE = "(123) 456-1111 or (222) 333-4444"
# Text in the beginning of the mail (if not specified, default text is used)
# This way text can be split to more lines
# Line breaks are done by '|' character
-MESSAGE = Hello, I've noticed you use too much space\
- on my disk|Delete your files on following filesystems:|
+# The expressions %i, %h, %d, and %% are substituted for user/group name,
+# host name, domain name, and '%' respectively. For backward compatibility
+# %s behaves as %i but is deprecated.
+MESSAGE = Hello user %i, I've noticed you use too much space\
+ on my disk in %h.%d.|Delete your files on the following filesystems:|
# Text in the end of the mail (if not specified, default text using SUPPORT and PHONE
# is created)
-SIGNATURE = See you!| Your admin|
+SIGNATURE = See you!| Your admin of %h|
# Following text is used for mails about group exceeding quotas
-# It should contain string %s exactly once - it will be substituted for a group name
-GROUP_MESSAGE = Hello, a group '%s' you're member of use too much space.|\
-I chose you to do the cleanup.|Delete group files on following filesystems:|
+GROUP_MESSAGE = Hello, a group '%i' you're member of use too much space at %h.|\
+I chose you to do the cleanup.|Delete group files on the following filesystems:|
# Text in the end of the mail to the group (if not specified, default text using SUPPORT
# and PHONE is created).
GROUP_SIGNATURE = See you!| Your admin|