chiark / gitweb /
tmpdir: Split out new file of common utilities.
[checkpath] / utils.c
diff --git a/utils.c b/utils.c
new file mode 100644 (file)
index 0000000..883a219
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,86 @@
+/* -*-c-*-
+ *
+ * Utilities not worth librarifying
+ *
+ * (c) 2008 Mark Wooding
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of chkpath.
+ *
+ * chkpath is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * chkpath is distributed 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with chkpath; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "config.h"
+
+#include <ctype.h>
+
+#include <sys/types.h>
+
+#include <grp.h>
+
+#include <mLib/macros.h>
+#include <mLib/report.h>
+
+#include "checkpath.h"
+#include "utils.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @allowgroup@ --- *
+ *
+ * Arguments:  @struct checkpath *cp@ = pointer to structure to mess with
+ *             @const char *gname@ = trust group @gname@
+ *
+ * Returns:    ---
+ *
+ * Use:                Adds the gid corresponding to @gname@ (which may be a number)
+ *             to the list of things we trust.
+ */
+
+void allowgroup(struct checkpath *cp, const char *gname)
+{
+  struct group *gr;
+  const char *p;
+  gid_t g;
+
+  /* --- Check for numeric group spec --- */
+
+  for (p = gname; *p; p++) {
+    if (!isdigit((unsigned char)*p))
+      goto lookup;
+  }
+  g = atoi(gname);
+  goto insert;
+
+  /* --- Look up a group by name --- */
+
+lookup:
+  if ((gr = getgrnam(gname)) == 0)
+    die(1, "group %s not found", gname);
+  g = gr->gr_gid;
+
+  /* --- Insert the group into the table --- */
+
+insert:
+  if (cp->cp_gids >= N(cp->cp_gid))
+    die(1, "too many groups");
+  cp->cp_gid[cp->cp_gids++] = g;
+}
+
+/*----- That's all, folks -------------------------------------------------*/