tell ceod when it is a club rep; club reps don't need the new member email
[mspang/pyceo.git] / src / op-adduser.c
index b1465cc..91d557f 100644 (file)
@@ -32,6 +32,7 @@ static const int MAX_MESGSIZE = 512;
 char *user_types[] = {
     [CEO__ADD_USER__TYPE__MEMBER] = "member",
     [CEO__ADD_USER__TYPE__CLUB] = "club",
+    [CEO__ADD_USER__TYPE__CLUB_REP] = "clubrep",
 };
 
 Ceo__AddUserResponse *response_create(void) {
@@ -42,6 +43,7 @@ Ceo__AddUserResponse *response_create(void) {
     return r;
 }
 
+PRINTF_LIKE(2)
 int32_t response_message(Ceo__AddUserResponse *r, int32_t status, char *fmt, ...) {
     va_list args;
     Ceo__StatusMessage *statusmsg = xmalloc(sizeof(Ceo__StatusMessage));
@@ -93,16 +95,22 @@ static int check_adduser(Ceo__AddUser *in, Ceo__AddUserResponse *out, char *clie
     if (!in->realname)
         return response_message(out, EINVAL, "missing required argument: realname");
 
-    if (in->type == CEO__ADD_USER__TYPE__MEMBER) {
-        if (!in->password)
-            return response_message(out, EINVAL, "missing required argument: password");
-    } else if (in->type == CEO__ADD_USER__TYPE__CLUB) {
-        if (in->password)
-            return response_message(out, EINVAL, "club accounts cannot have passwords");
-        if (in->program)
-            return response_message(out, EINVAL, "club accounts cannot have programs");
-    } else {
-        return response_message(out, EINVAL, "invalid user type: %d", in->type);
+    switch (in->type) {
+        case CEO__ADD_USER__TYPE__MEMBER:
+        case CEO__ADD_USER__TYPE__CLUB_REP:
+            if (!in->password)
+                return response_message(out, EINVAL, "missing required argument: password");
+            break;
+
+        case CEO__ADD_USER__TYPE__CLUB:
+            if (in->password)
+                return response_message(out, EINVAL, "club accounts cannot have passwords");
+            if (in->program)
+                return response_message(out, EINVAL, "club accounts cannot have programs");
+            break;
+
+        default:
+            return response_message(out, EINVAL, "invalid user type: %d", in->type);
     }
 
     if (getpwnam(in->username) != NULL)
@@ -138,7 +146,7 @@ static void adduser_spam(Ceo__AddUser *in, Ceo__AddUserResponse *out, char *clie
 static int32_t addmember(Ceo__AddUser *in, Ceo__AddUserResponse *out) {
     char homedir[1024];
     char principal[1024];
-    int user_stat, group_stat, krb_stat, home_stat;
+    int user_stat, group_stat, krb_stat, home_stat, quota_stat;
     int id;
 
     if (snprintf(principal, sizeof(principal), "%s@%s",
@@ -171,27 +179,34 @@ static int32_t addmember(Ceo__AddUser *in, Ceo__AddUserResponse *out) {
     else
         response_message(out, 0, "successfully created ldap group");
 
-    if ((home_stat = ceo_create_home(homedir, member_home_skel,  id, id)))
+    if ((home_stat = ceo_create_home(homedir, member_home_skel, id, id, NULL, NULL, in->email)))
         response_message(out, EHOME, "unable to create home directory for %s", in->username);
     else
         response_message(out, 0, "successfully created home directory");
 
+    if ((quota_stat = ceo_set_quota("ctdalek", id)))
+        response_message(out, EQUOTA, "unable to set quota for %s", in->username);
+    else
+        response_message(out, 0, "successfully set quota");
 
-    return krb_stat || user_stat || group_stat || home_stat;
+    return krb_stat || user_stat || group_stat || home_stat || quota_stat;
 }
 
 static int32_t addclub(Ceo__AddUser *in, Ceo__AddUserResponse *out) {
     char homedir[1024];
-    int krb_stat, user_stat, group_stat, sudo_stat, home_stat;
+    char acl[64];
+    int krb_stat, user_stat, group_stat, sudo_stat, home_stat, quota_stat;
     int id;
 
-    if (snprintf(homedir, sizeof(homedir), "%s/%s",
-                 club_home, in->username) >= sizeof(homedir))
+    if (snprintf(homedir, sizeof(homedir), "%s/%s", club_home, in->username) >= sizeof(homedir))
         fatal("homedir overflow");
 
     if ((id = ceo_new_uid(club_min_id, club_max_id)) <= 0)
         fatal("no available uids in range [%ld, %ld]", club_min_id, club_max_id);
 
+    if (snprintf(acl, sizeof(acl), CLUB_ACL, id) >= sizeof(acl))
+        fatal("acl overflow");
+
     if ((krb_stat = ceo_del_princ(in->username)))
         return response_message(out, EKERB, "unable to clear principal %s", in->username);
 
@@ -212,12 +227,17 @@ static int32_t addclub(Ceo__AddUser *in, Ceo__AddUserResponse *out) {
     else
         response_message(out, 0, "successfully created ldap sudoers");
 
-    if ((home_stat = ceo_create_home(homedir, club_home_skel, id, id)))
+    if ((home_stat = ceo_create_home(homedir, club_home_skel, id, id, acl, acl, NULL)))
         response_message(out, EHOME, "unable to create home directory for %s", in->username);
     else
         response_message(out, 0, "successfully created home directory");
 
-    return user_stat || group_stat || sudo_stat || home_stat;
+    if ((quota_stat = ceo_set_quota("csc", id)))
+        response_message(out, EQUOTA, "unable to set quota for %s", in->username);
+    else
+        response_message(out, 0, "successfully set quota");
+
+    return user_stat || group_stat || sudo_stat || home_stat || quota_stat;
 }
 
 static int32_t adduser(Ceo__AddUser *in, Ceo__AddUserResponse *out, char *client) {
@@ -231,6 +251,9 @@ static int32_t adduser(Ceo__AddUser *in, Ceo__AddUserResponse *out, char *client
     if (in->type == CEO__ADD_USER__TYPE__MEMBER) {
         status = addmember(in, out);
         prog = "addmember";
+    } else if (in->type == CEO__ADD_USER__TYPE__CLUB_REP) {
+        status = addmember(in, out);
+        prog = "addclubrep";
     } else if (in->type == CEO__ADD_USER__TYPE__CLUB) {
         status = addclub(in, out);
         prog = "addclub";
@@ -268,7 +291,9 @@ void cmd_adduser(void) {
 
     strbuf_grow(&out, ceo__add_user_response__get_packed_size(out_proto));
     strbuf_setlen(&out, ceo__add_user_response__pack(out_proto, (uint8_t *)out.buf));
-    full_write(STDOUT_FILENO, out.buf, out.len);
+
+    if (full_write(STDOUT_FILENO, out.buf, out.len))
+        fatalpe("write: stdout");
 
     ceo__add_user__free_unpacked(in_proto, &protobuf_c_default_allocator);
     response_delete(out_proto);
@@ -279,7 +304,7 @@ void cmd_adduser(void) {
 
 int main(int argc, char *argv[]) {
     prog = xstrdup(basename(argv[0]));
-    init_log(prog, LOG_PID, LOG_AUTHPRIV);
+    init_log(prog, LOG_PID, LOG_AUTHPRIV, 0);
 
     configure();